package com.example.terminal.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.terminal.R;
import com.example.terminal.base.BaseActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 表单页面
 */
public class FormActivity extends BaseActivity {

    private String jsonData = "{\n" +
            "  \"fields\": [\n" +
            "    {\n" +
            "      \"type\": \"text\",\n" +
            "      \"label\": \"Name\",\n" +
            "      \"hint\": \"Enter your name\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"type\": \"email\",\n" +
            "      \"label\": \"Email\",\n" +
            "      \"hint\": \"Enter your email\"\n" +
            "    },\n" +
            "    {\n" +
            "      \"type\": \"password\",\n" +
            "      \"label\": \"Password\",\n" +
            "      \"hint\": \"Enter your password\"\n" +
            "    }\n" +
            "  ]\n" +
            "}";

    @Override
    protected int setContentView() {
        return R.layout.activity_form;
    }

    @Override
    protected void initView() {
        LinearLayout formLayout = findViewById(R.id.formLayout);

        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            JSONArray fieldsArray = jsonObject.getJSONArray("fields");

            for (int i = 0; i < fieldsArray.length(); i++) {
                JSONObject fieldObject = fieldsArray.getJSONObject(i);
                String type = fieldObject.getString("type");
                String label = fieldObject.getString("label");
                String hint = fieldObject.getString("hint");

                // 根据类型创建相应的表单元素
                if ("text".equals(type) || "email".equals(type) || "password".equals(type)) {
                    EditText editText = new EditText(this);
                    editText.setHint(hint);
                    // 可以根据类型设置输入类型，如email、password等
                    // editText.setInputType(...);

                    TextView labelTextView = new TextView(this);
                    labelTextView.setText(label);

                    // 将标签和输入框添加到布局中
                    formLayout.addView(labelTextView);
                    formLayout.addView(editText);
                }
                // 可以根据需要扩展以支持更多类型的字段
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initData() {

    }
}